#include "measurepagecharts.h"
#include "Config/constant.h"
MeasurePageCharts::MeasurePageCharts()
{
    Series_PPG1 = new QLineSeries;
    Series_PPG2 = new QLineSeries;
    Series_APPG1 = new QLineSeries;
    Series_APPG2 = new QLineSeries;
    LineChartLen_X=1*Constant::SampleRate;//一屏显示5秒钟的数据
    HRVBarNum=30;//一屏显示30个hrv柱形图
    Chart_Y_Axis_Line_Color=new QColor(0xC4,0x91,0x44,0x20);
    Font_Chart_LabelsFont=new QFont("微软雅黑", 6, QFont::Normal, false);
    Font_Chart_TitleFont=new QFont("微软雅黑", 12, QFont::Normal, false);
    ppg1_flag=0;
    ppg2_flag=0;
    app1_flag=0;
    appg2_flag=0;
    InitialAxis_Chart_PPG1();
    InitialAxis_Chart_APPG1();
    InitialAxis_Chart_HRV1();

    InitialAxis_Chart_PPG2();
    InitialAxis_Chart_APPG2();
    InitialAxis_Chart_HRV2();

}
MeasurePageCharts::~MeasurePageCharts()
{
    delete Series_PPG1;
    delete Series_PPG2;
    delete Series_APPG1;
    delete Series_APPG2;
    delete Chart_Y_Axis_Line_Color;
    delete Font_Chart_LabelsFont;
    delete Font_Chart_TitleFont;
    delete xAxis_PPG1;
    delete yAxis_PPG1;
    delete xAxis_PPG2;
    delete yAxis_PPG2;

    delete xAxis_APPG1;
    delete xAxis_APPG2;
    delete yAxis_APPG1;
    delete yAxis_APPG2;
    delete xAxis_HRV1;
    delete xAxis_HRV2;
    delete yAxis_HRV1;
    delete yAxis_HRV2;

    delete HRVHist_BarSet;
    delete HRVHist_BarSet2;

}

void MeasurePageCharts::InitialAxis_Chart_PPG1()
{
    xAxis_PPG1 = new QValueAxis();
    yAxis_PPG1 = new QValueAxis();
    yAxis_PPG1->setRange(-1, 1);
    xAxis_PPG1->setRange(0, LineChartLen_X);
    xAxis_PPG1->setTitleFont(*Font_Chart_TitleFont);
    yAxis_PPG1->setTitleFont(*Font_Chart_TitleFont);
    xAxis_PPG1->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_PPG1->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_PPG1->setTitleText("mv");
    yAxis_PPG1->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_PPG1->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_PPG1->setGridLineVisible(false);
    yAxis_PPG1->setGridLineVisible(true);
    yAxis_PPG1->setGridLineColor(*Chart_Y_Axis_Line_Color);
    xAxis_PPG1->setLineVisible(false);
    xAxis_PPG1->setVisible(false);
    yAxis_PPG1->setVisible(true);
}
/**
 * @brief MeasurePageCharts::CreateLineChart_PPG1
 * @return
 */
QChart *MeasurePageCharts::CreateLineChart_PPG1()
{
    QChart *chart = new QChart();
    QColor(0x96,0x88,0xFF);
    Series_PPG1->setPen(QPen(QColor(0xFF,0x70,0x0F),2));//设置波形颜色
//    Series_PPG1->clear();
    chart->addSeries(Series_PPG1);
    chart->setBackgroundVisible(true);
//    chart->setBackgroundBrush(QBrush(QColor(0xF0,0xF6,0xF9)));
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_PPG1, Qt::AlignBottom);
    chart->addAxis(yAxis_PPG1, Qt::AlignLeft);
    Series_PPG1->attachAxis(xAxis_PPG1);
    Series_PPG1->attachAxis(yAxis_PPG1);
    return chart;
}
/**
 * @brief MeasurePageCharts::Update_PPGData1
 * 功能：更新P1脉搏波图表，绘图
 */
void MeasurePageCharts::Update_PPGData1(double ppg)
{
    ppg1_flag++;
    QVector<QPointF> oldData = Series_PPG1->points();
    QVector<QPointF> data;
    if (oldData.size() < LineChartLen_X) {
        data = Series_PPG1->points();
    } else {
        for (int i = 1; i < oldData.size(); ++i) {
            data.append(QPointF(i - 1 , oldData.at(i).y()));
        }
    }
    qint64 size = data.size();
    data.append(QPointF(size, ppg));
    Series_PPG1->replace(data);
    //自动调整波形图表
    if(ppg1_flag%100==0){
        double maxv=data.at(0).y();
        double minv=data.at(0).y();
        for(int i=0;i<size;i++)
        {
            if(data.at(i).y()>maxv)
                maxv=data.at(i).y();
            if(data.at(i).y()<minv)
                minv=data.at(i).y();
        }
        yAxis_PPG1->setRange(minv,maxv);
    }
}


void MeasurePageCharts::InitialAxis_Chart_PPG2()
{
    xAxis_PPG2 = new QValueAxis();
    yAxis_PPG2 = new QValueAxis();
    yAxis_PPG2->setRange(-1, 1);
    xAxis_PPG2->setRange(0, LineChartLen_X);
    xAxis_PPG2->setTitleFont(*Font_Chart_TitleFont);
    yAxis_PPG2->setTitleFont(*Font_Chart_TitleFont);
    xAxis_PPG2->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_PPG2->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_PPG2->setTitleText("mv");
    yAxis_PPG2->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_PPG2->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_PPG2->setGridLineVisible(false);
    yAxis_PPG2->setGridLineVisible(true);
    yAxis_PPG2->setGridLineColor(*Chart_Y_Axis_Line_Color);
    xAxis_PPG2->setLineVisible(false);
    xAxis_PPG2->setVisible(false);
    yAxis_PPG2->setVisible(true);
}
/**
 * @brief MeasurePageCharts::CreateLineChart_PPG1
 * @return
 */
QChart *MeasurePageCharts::CreateLineChart_PPG2()
{
    QChart *chart = new QChart();
    QColor(0x96,0x88,0xFF);
    Series_PPG2->setPen(QPen(QColor(0xFF,0x70,0x0F),2));//设置波形颜色
    //    Series_PPG1->clear();
    chart->addSeries(Series_PPG2);
    chart->setBackgroundVisible(true);
    //    chart->setBackgroundBrush(QBrush(QColor(0xF0,0xF6,0xF9)));
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_PPG2, Qt::AlignBottom);
    chart->addAxis(yAxis_PPG2, Qt::AlignLeft);
    Series_PPG2->attachAxis(xAxis_PPG2);
    Series_PPG2->attachAxis(yAxis_PPG2);
    return chart;
}
/**
 * @brief MeasurePageCharts::Update_PPGData1
 * 功能：更新P1脉搏波图表，绘图
 */
void MeasurePageCharts::Update_PPGData2(double ppg)
{
    ppg2_flag++;
    QVector<QPointF> oldData = Series_PPG2->points();
    QVector<QPointF> data;
    if (oldData.size() < LineChartLen_X) {
        data = Series_PPG2->points();
    } else {
        for (int i = 1; i < oldData.size(); ++i) {
            data.append(QPointF(i - 1 , oldData.at(i).y()));
        }
    }
    qint64 size = data.size();
    data.append(QPointF(size, ppg));
    Series_PPG2->replace(data);
    //自动调整波形图表
    if(ppg2_flag%100==0){
        double maxv=data.at(0).y();
        double minv=data.at(0).y();
        for(int i=0;i<size;i++)
        {
            if(data.at(i).y()>maxv)
                maxv=data.at(i).y();
            if(data.at(i).y()<minv)
                minv=data.at(i).y();
        }
        yAxis_PPG2->setRange(minv,maxv);
    }
}


void MeasurePageCharts::InitialAxis_Chart_APPG1()
{
    xAxis_APPG1 = new QValueAxis();
    yAxis_APPG1 = new QValueAxis();
    yAxis_APPG1->setRange(-1, 1);
    xAxis_APPG1->setRange(0, LineChartLen_X);
    xAxis_APPG1->setTitleFont(*Font_Chart_TitleFont);
    yAxis_APPG1->setTitleFont(*Font_Chart_TitleFont);
    xAxis_APPG1->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_APPG1->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_APPG1->setTitleText("mv");
    yAxis_APPG1->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_APPG1->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_APPG1->setGridLineVisible(false);
    yAxis_APPG1->setGridLineVisible(true);
    yAxis_APPG1->setGridLineColor(*Chart_Y_Axis_Line_Color);
    xAxis_APPG1->setLineVisible(false);
    xAxis_APPG1->setVisible(false);
    yAxis_APPG1->setVisible(true);
}
/**
 * @brief MeasurePageCharts::CreateLineChart_PPG1
 * @return
 */
QChart *MeasurePageCharts::CreateLineChart_APPG1()
{
    QChart *chart = new QChart();
    QColor(0x96,0x88,0xFF);
    Series_APPG1->setPen(QPen(QColor(0x3C,0xB4,0x4D),2));//设置波形颜色
    chart->addSeries(Series_APPG1);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_APPG1, Qt::AlignBottom);
    chart->addAxis(yAxis_APPG1, Qt::AlignLeft);
    Series_APPG1->attachAxis(xAxis_APPG1);
    Series_APPG1->attachAxis(yAxis_APPG1);
    return chart;
}
/**
 * @brief MeasurePageCharts::Update_PPGData1
 * 功能：更新P1脉搏波图表，绘图
 */
void MeasurePageCharts::Update_APPGData1(double ppg)
{
    app1_flag++;
    QVector<QPointF> oldData = Series_APPG1->points();
    QVector<QPointF> data;
    if (oldData.size() < LineChartLen_X) {
        data = Series_APPG1->points();
    } else {
        for (int i = 1; i < oldData.size(); ++i) {
            data.append(QPointF(i - 1 , oldData.at(i).y()));
        }
    }
    qint64 size = data.size();
    data.append(QPointF(size, ppg));
    Series_APPG1->replace(data);
    //自动调整波形图表
    if(app1_flag%100==0){
        double maxv=data.at(0).y();
        double minv=data.at(0).y();
        for(int i=0;i<size;i++)
        {
            if(data.at(i).y()>maxv)
                maxv=data.at(i).y();
            if(data.at(i).y()<minv)
                minv=data.at(i).y();
        }
        yAxis_APPG1->setRange(minv,maxv);
    }
}

void MeasurePageCharts::InitialAxis_Chart_APPG2()
{
    xAxis_APPG2 = new QValueAxis();
    yAxis_APPG2 = new QValueAxis();
    yAxis_APPG2->setRange(-1, 1);
    xAxis_APPG2->setRange(0, LineChartLen_X);
    xAxis_APPG2->setTitleFont(*Font_Chart_TitleFont);
    yAxis_APPG2->setTitleFont(*Font_Chart_TitleFont);
    xAxis_APPG2->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_APPG2->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_APPG2->setTitleText("mv");
    yAxis_APPG2->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_APPG2->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_APPG2->setGridLineVisible(false);
    yAxis_APPG2->setGridLineVisible(true);
    yAxis_APPG2->setGridLineColor(*Chart_Y_Axis_Line_Color);
    xAxis_APPG2->setLineVisible(false);
    xAxis_APPG2->setVisible(false);
    yAxis_APPG2->setVisible(true);
}
/**
 * @brief MeasurePageCharts::CreateLineChart_PPG1
 * @return
 */
QChart *MeasurePageCharts::CreateLineChart_APPG2()
{
    QChart *chart = new QChart();
    QColor(0x96,0x88,0xFF);
    Series_APPG2->setPen(QPen(QColor(0x3C,0xB4,0x4D),2));//设置波形颜色
    chart->addSeries(Series_APPG2);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_APPG2, Qt::AlignBottom);
    chart->addAxis(yAxis_APPG2, Qt::AlignLeft);
    Series_APPG2->attachAxis(xAxis_APPG2);
    Series_APPG2->attachAxis(yAxis_APPG2);
    return chart;
}
/**
 * @brief MeasurePageCharts::Update_PPGData2
 * 功能：更新P1脉搏波图表，绘图
 */
void MeasurePageCharts::Update_APPGData2(double ppg)
{
    appg2_flag++;
    QVector<QPointF> oldData = Series_APPG2->points();
    QVector<QPointF> data;
    if (oldData.size() < LineChartLen_X) {
        data = Series_APPG2->points();
    } else {
        for (int i = 1; i < oldData.size(); ++i) {
            data.append(QPointF(i - 1 , oldData.at(i).y()));
        }
    }
    qint64 size = data.size();
    data.append(QPointF(size, ppg));
    Series_APPG2->replace(data);
    //自动调整波形图表
    if(appg2_flag%100==0){
        double maxv=data.at(0).y();
        double minv=data.at(0).y();
        for(int i=0;i<size;i++)
        {
            if(data.at(i).y()>maxv)
                maxv=data.at(i).y();
            if(data.at(i).y()<minv)
                minv=data.at(i).y();
        }
        yAxis_APPG2->setRange(minv,maxv);
    }
}


void MeasurePageCharts::InitialAxis_Chart_HRV1()
{
    xAxis_HRV1 = new QValueAxis();
    yAxis_HRV1 = new QValueAxis();
    yAxis_HRV1->setRange(0, 1500);
    xAxis_HRV1->setRange(-1, HRVBarNum);
    xAxis_HRV1->setTitleFont(*Font_Chart_TitleFont);
    yAxis_HRV1->setTitleFont(*Font_Chart_TitleFont);
    xAxis_HRV1->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_HRV1->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_HRV1->setTitleText("ms");
    yAxis_HRV1->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_HRV1->setLabelsColor(QColor(0x2E,0x5A,0x82));
    xAxis_HRV1->setGridLineVisible(false);
    yAxis_HRV1->setGridLineVisible(true);
    yAxis_HRV1->setGridLineColor(*Chart_Y_Axis_Line_Color);
    xAxis_HRV1->setLineVisible(false);
    xAxis_HRV1->setVisible(false);
    yAxis_HRV1->setVisible(true);
}
QChart *MeasurePageCharts::CreateHRVHistBarChart()
{
    QChart *chart = new QChart();
    QColor(0x96,0x88,0xFF);
    HRVHist_BarSet = new QBarSet("");
    QLinearGradient linearGradient(0, 50, 0, 150);//初始化，设置开始和结束点
    linearGradient.setColorAt(0.3, QColor(0x00,0x90,0xFF));//设置从0-30%的区域为红色
    //linearGradient.setColorAt(0.6, QColor(0x3C,0xB4,0x4D));//设置30%-60%的区域为绿色
    linearGradient.setColorAt(1.0, QColor(0x6A,0xDA,0xFF));//设置60%-100%的区域为蓝色
    HRVHist_BarSet->setBrush(QBrush(linearGradient));

    chart->setAnimationOptions(QChart::SeriesAnimations);
    QBarSeries *HRV_Barseries = new QBarSeries();
    for(int i=0;i<HRVBarNum;i++){
        HRVHist_BarSet->append(0);
    }
    HRV_Barseries->append(HRVHist_BarSet);
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->addSeries(HRV_Barseries);
    chart->setTitle("");
    chart->addAxis(xAxis_HRV1, Qt::AlignBottom);
    chart->addAxis(yAxis_HRV1, Qt::AlignLeft);
    chart->legend()->setVisible(false);
    HRV_Barseries->attachAxis(xAxis_HRV1);
    HRV_Barseries->attachAxis(yAxis_HRV1);
    return chart;
}

void MeasurePageCharts::Update_HRVHistBarData(int pos,int RR)
{
    if(pos==0)
    {
        for(int i=0;i<HRVBarNum;i++)
        {
            HRVHist_BarSet->replace(i,0);
        }
    }
    HRVHist_BarSet->replace(pos,RR);
}


void MeasurePageCharts::InitialAxis_Chart_HRV2()
{
    xAxis_HRV2 = new QValueAxis();
    yAxis_HRV2 = new QValueAxis();
    yAxis_HRV2->setRange(0, 1500);
    xAxis_HRV2->setRange(-1, HRVBarNum);
    xAxis_HRV2->setTitleFont(*Font_Chart_TitleFont);
    yAxis_HRV2->setTitleFont(*Font_Chart_TitleFont);
    xAxis_HRV2->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_HRV2->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_HRV2->setTitleText("ms");
    yAxis_HRV2->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_HRV2->setLabelsColor(QColor(0x2E,0x5A,0x82));
    xAxis_HRV2->setGridLineVisible(false);
    yAxis_HRV2->setGridLineVisible(true);
    yAxis_HRV2->setGridLineColor(*Chart_Y_Axis_Line_Color);
    xAxis_HRV2->setLineVisible(false);
    xAxis_HRV2->setVisible(false);
    yAxis_HRV2->setVisible(true);
}
QChart *MeasurePageCharts::CreateHRVHistBarChart2()
{
    QChart *chart = new QChart();
    QColor(0x96,0x88,0xFF);
    HRVHist_BarSet2 = new QBarSet("");
    QLinearGradient linearGradient(0, 50, 0, 150);//初始化，设置开始和结束点
    linearGradient.setColorAt(0.3, QColor(0x00,0x90,0xFF));//设置从0-30%的区域为红色
    //linearGradient.setColorAt(0.6, QColor(0x3C,0xB4,0x4D));//设置30%-60%的区域为绿色
    linearGradient.setColorAt(1.0, QColor(0x6A,0xDA,0xFF));//设置60%-100%的区域为蓝色
    HRVHist_BarSet2->setBrush(QBrush(linearGradient));

    chart->setAnimationOptions(QChart::SeriesAnimations);
    QBarSeries *HRV_Barseries = new QBarSeries();
    for(int i=0;i<HRVBarNum;i++){
        HRVHist_BarSet2->append(0);
    }
    HRV_Barseries->append(HRVHist_BarSet2);
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->addSeries(HRV_Barseries);
    chart->setTitle("");
    chart->addAxis(xAxis_HRV2, Qt::AlignBottom);
    chart->addAxis(yAxis_HRV2, Qt::AlignLeft);
    chart->legend()->setVisible(false);
    HRV_Barseries->attachAxis(xAxis_HRV2);
    HRV_Barseries->attachAxis(yAxis_HRV2);
    return chart;
}

void MeasurePageCharts::Update_HRVHistBarData2(int pos,int RR)
{
    if(pos==0)
    {
        for(int i=0;i<HRVBarNum;i++)
        {
            HRVHist_BarSet2->replace(i,0);
        }
    }
    HRVHist_BarSet2->replace(pos,RR);
}
